package com.youlai.quarkus.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.quarkus.system.common.anno.annotation.PreventDuplicateSubmit;
import com.youlai.quarkus.system.common.model.Option;
import com.youlai.quarkus.system.common.result.PageResult;
import com.youlai.quarkus.system.common.result.Result;
import com.youlai.quarkus.system.model.form.RoleForm;
import com.youlai.quarkus.system.model.query.RolePageQuery;
import com.youlai.quarkus.system.model.vo.RolePageVO;
import com.youlai.quarkus.system.service.SysRoleService;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import lombok.RequiredArgsConstructor;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.util.List;

@Tag(name = "03.角色接口")
@Path("/api/v1/roles")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService roleService;

    @Operation(summary = "角色分页列表")
    @Path("/page")
    @GET
    public PageResult<RolePageVO> getRolePage(
            @QueryParam(value = "pageNum") Integer pageNum,
            @QueryParam(value = "pageSize") Integer pageSize,
            @QueryParam(value = "keywords") String keywords
    ) {
        RolePageQuery queryParams = new RolePageQuery(keywords);
        queryParams.setPageNum(pageNum);
        queryParams.setPageSize(pageSize);
        Page<RolePageVO> result = roleService.getRolePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "角色下拉列表")
    @Path("/options")
    @GET
    public Result<List<Option>> listRoleOptions() {
        List<Option> list = roleService.listRoleOptions();
        return Result.success(list);
    }
 
    @Operation(summary = "新增角色")
    @POST
    @PreventDuplicateSubmit
    public Result addRole(@Valid @RequestBody RoleForm roleForm) {
        boolean result = roleService.saveRole(roleForm);
        return Result.judge(result);
    }

    @Operation(summary = "角色表单数据")
    @Path("/{roleId}/form")
    @GET
    public Result<RoleForm> getRoleForm(
            @Parameter(description = "角色ID") @PathParam("roleId") Long roleId
    ) {
        RoleForm roleForm = roleService.getRoleForm(roleId);
        return Result.success(roleForm);
    }

    @Operation(summary = "修改角色")
    @Path(value = "/{id}")
    @PUT
    public Result updateRole(@Valid @RequestBody RoleForm roleForm) {
        boolean result = roleService.saveRole(roleForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除角色")
    @Path("/{ids}")
    @DELETE
    public Result deleteRoles(
            @Parameter(description ="删除角色，多个以英文逗号(,)分割") @PathParam("ids") String ids
    ) {
        boolean result = roleService.deleteRoles(ids);
        return Result.judge(result);
    }

    @Operation(summary = "修改角色状态")
    @Path(value = "/{roleId}/status")
    @PUT
    public Result updateRoleStatus(
            @Parameter(description ="角色ID") @PathParam("roleId") Long roleId,
            @Parameter(description ="状态(1:启用;0:禁用)") @FormParam("status") Integer status
    ) {
        boolean result = roleService.updateRoleStatus(roleId, status);
        return Result.judge(result);
    }

    @Operation(summary = "获取角色的菜单ID集合")
    @Path("/{roleId}/menuIds")
    @GET
    public Result<List<Long>> getRoleMenuIds(
            @Parameter(description ="角色ID") @PathParam("roleId") Long roleId
    ) {
        List<Long> menuIds = roleService.getRoleMenuIds(roleId);
        return Result.success(menuIds);
    }

    @Operation(summary = "分配菜单权限给角色")
    @Path("/{roleId}/menus")
    @PUT
    public Result updateRoleMenus(
            @PathParam("roleId") Long roleId,
            @RequestBody List<Long> menuIds
    ) {
        boolean result = roleService.updateRoleMenus(roleId,menuIds);
        return Result.judge(result);
    }
}
